package com.yc.book.biz;

import java.sql.Date;
import java.sql.SQLException;

import com.yc.book.dao.BookDao;
import com.yc.book.util.Utils;

public class BookBiz {
	
	private BookDao bookDao = new BookDao();
	public void save(String id, String name, String author, Date pressdate, int price)
			throws BizException {
		// 数据校验
		//		if(name == null && name.trim().isEmpty()) {
		//			throw new BizException("请输入书名");
		//		}
		Utils.check(Utils.isEmpty(name), "请输入书名");
		Utils.check(name.length() < 2 || name.length() > 50, "书名字数必须是2~50个字");

		Utils.check(Utils.isEmpty(author), "请输入作者");
		Utils.check(author.length() < 2 || author.length() > 20, "作者字数必须是2~20个字");
		
		// 其他字段自行验证
		try {
			if(Utils.isEmpty(id)) {
				bookDao.insert(id, name, author, pressdate, price);
			} else {
				bookDao.update(id, name, author, pressdate, price);
			}
		} catch (SQLException e) {
			throw new BizException("系统异常请联系管理员!", e);
		}
		
	}

}
